const curry = require("./template.js")

function testCurry() {
  // 测试用的基本函数
  const add = (a, b, c) => a + b + c
  const multiply = (x, y) => x * y
  const greet = (greeting, name, punctuation) =>
    `${greeting}, ${name}${punctuation}`

  console.log(
    "1. 基本柯里化:",
    curry(add)(1)(2)(3) === 6 &&
      curry(multiply)(3)(4) === 12 &&
      curry(greet)("Hello")("World")("!") === "Hello, World!"
  )

  console.log(
    "2. 部分应用:",
    (() => {
      const curriedAdd = curry(add)
      const addFive = curriedAdd(5)
      const addFiveAndTwo = addFive(2)
      return addFiveAndTwo(3) === 10
    })()
  )

  console.log(
    "3. 多种调用方式:",
    curry(add)(1, 2)(3) === 6 &&
      curry(add)(1)(2, 3) === 6 &&
      curry(add)(1, 2, 3) === 6
  )

  console.log(
    "4. 函数复用:",
    (() => {
      const curriedMultiply = curry(multiply)
      const double = curriedMultiply(2)
      const triple = curriedMultiply(3)
      return double(5) === 10 && triple(4) === 12
    })()
  )

  console.log(
    "5. 单参数函数:",
    (() => {
      const square = (x) => x * x
      const curriedSquare = curry(square)
      return curriedSquare(5) === 25
    })()
  )

  console.log(
    "6. 零参数处理:",
    (() => {
      const noParams = () => "no params"
      const curriedNoParams = curry(noParams)
      return curriedNoParams() === "no params"
    })()
  )

  console.log(
    "7. 上下文保持:",
    (() => {
      const obj = {
        value: 10,
        add: function (a, b) {
          return this.value + a + b
        }
      }
      const curriedAdd = curry(obj.add.bind(obj))
      return curriedAdd(5)(15) === 30
    })()
  )

  console.log(
    "8. 复杂嵌套调用:",
    (() => {
      const complexFn = (a, b, c, d) => a * b + c - d
      const curriedComplex = curry(complexFn)
      return curriedComplex(2)(3)(4)(1) === 9 // 2*3+4-1 = 9
    })()
  )
}

testCurry()
